package com.supermap.dataservice.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author aqua
 * @desc 工具包
 * @date 2019年05月22 15:52
 * @email 1301584033@qq.com
 */
@Component
public class JedisComponent {

    @Autowired
    JedisPool jedisPool;

    public  void setObject(String key,Object object){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serizlize(object));
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public  Boolean exists(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return  jedis.exists(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public  Object getObject(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            return SerializeUtil.deserialize(bytes);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public void setMap(String key, Map map) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key,map);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public Map getMap(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
           return  jedis.hgetAll(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    public void set(String key, String val) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, val);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public void setKeyTime(String key,int time) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.expire(key,time);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public void delKey(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public Long getKeyTime(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
           return jedis.ttl(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public Long removeKeyTime(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.persist(key);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

}
